﻿using Dapper;
using OMG.Fill.Potato.Model.Ent;
using ProjectShare;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OMG.Fill.Potato.Service
{
    public class DataDict
    {
        private static DataDict _instance = new DataDict();
        public static DataDict Instance { get { return _instance; } }
        private DataDict() { }
        Dictionary<string, Dictionary<string, string>> _dict = new Dictionary<string, Dictionary<string, string>>();
        public Dictionary<string, string> this[string key]
        {
            get
            {
                using (var dbCon = ConnectFactory.GetOpenConnection())
                {
                    Dictionary<string, string> tempDict = null;
                    if (!_dict.ContainsKey(key))
                    {
                        tempDict = new Dictionary<string, string>();
                        string strSql = "select DictText,DictValue from dbo.T_Dict where GroupCode=@GroupCode ";
                        List<DictEntity> templist = dbCon.Query<DictEntity>(strSql, new { GroupCode = key }).ToList();
                        foreach (var item in templist)
                        {
                            tempDict.Add(item.DictValue, item.DictText);
                        }

                        _dict.Add(key, tempDict);
                    }
                    else
                    {
                        tempDict = _dict[key];
                    }
                    return tempDict;
                }
            }
        }
        public void Clear()
        {
            _dict.Clear();
        }

        public void Clear(string key)
        {
            if (_dict.ContainsKey(key))
            {
                _dict.Remove(key);
            }
        }
    }
}
